#1.注册


from api import user_i
from lib.common import is_login
from api import bank_i
login_user=None
def register():
    while 1:
        user_name=input('请输入你的用户名')
        password=int(input('请输入密码'))
        re_password=int(input('请确认你的密码'))
        if password==re_password:
            flag,msg=user_i.register_info(user_name,password)
            if flag:
                print(msg)
                break
            else:
                print('注册失败，继续注册')
        else:
            print('密码不一致，请重新输入')


#2.登录
def login():
    while 1:
        user_name = input('请输入你的用户名')
        password = int(input('请输入密码'))
        flag,msg=user_i.login_info(user_name,password)
        if flag:
            print(msg)
            global login_user
            login_user=user_name
            print(f'尊敬的{login_user}，欢迎您使用')
            break
        else:
            print(msg)
#3.查看余额
@is_login
def check_money():
    money=bank_i.select_money(login_user)
    print(f'{login_user}用户还剩{money}元')
#4.存款
@is_login

def save_money():
    while 1:
        money = float(input('请输入要存的钱'))
        flag,msg=bank_i.recharge(login_user,money)
        if flag:
            print(msg)
            break

#5.取款
@is_login

def get_money():
    while 1:
        money = float(input('请输入要取出的钱'))
        flag,msg=bank_i.take_money(login_user,money)
        if flag:
            print(msg)
            break
        else:
            print(msg)
#6.账单
@is_login

def account():
    user=bank_i.get_acc(login_user)
    if user:
        for i in user:
            print(i)
    else:
        print('暂无账单')

select_fun={
    0:['退出',exit],
    1:['注册',register],
    2:['登录',login],
    3:['查看余额',check_money],
    4:['存钱',save_money],
    5:['取钱',get_money],
    6:['账单',account]
}
def run():
    while 1:
        print('             *********           ')
        print('             *********           ')
        print('             欢迎来到ATM           ')
        print('             *********           ')
        print('             *********           ')
        for i in select_fun:
            print(i,select_fun[i][0])
        select = int(input('请输入你选择的序号（数字）'))
        if select<=6 and select>=0:
            select_fun[select][1]()
        else:
            print('操作数不存在，请重新输入')

